package com.dxl.lanju.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dxl.common.annotation.RequirePermission;
import com.dxl.common.enums.State;
import com.dxl.common.model.Order;
import com.dxl.common.model.Page;
import com.dxl.common.model.RespData;
import com.dxl.common.utils.Utils;
import com.dxl.lanju.service.AreaService;
import com.dxl.web.common.controller.BaseController;
import com.lanju.entity.Area;

/**
 * 区域信息控制器
 * @author 邓小林
 */
@Controller
@RequestMapping("/area")
@RequirePermission(value={"AREA-MODULE"}, name="区域模块", desc="包括区域增、删、改、查")
public class AreaController extends BaseController {
	@Autowired
	private AreaService areaService;
	
	@RequestMapping("")
	@RequirePermission(value={"AREA-VIEW"}, name="区域查看", desc="查看区域信息")
	public ModelAndView area() {
		ModelAndView mv = new ModelAndView("area/list");
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/list")
	@RequirePermission(value={"AREA-VIEW"}, name="区域查看", desc="查看区域信息")
	public Page<Area> list(String keyword, Integer state, Integer page, Integer rows) {
		Page<Area> p = this.areaService.list(keyword, state, page, rows);
		return p;
	}
	
	@ResponseBody
	@RequestMapping("/listAll")
	@RequirePermission(value={"AREA-VIEW"}, name="区域查看", desc="查看区域信息")
	public List<Area> listAll(Integer state) {
		return areaService.listByProp(Area.class, "state", Utils.isNull(state) ? State.ACTIVE.getState() : state, Order.asc("name"));
	}
	
	@RequestMapping("/edit")
	@RequirePermission(value={"AREA-VIEW"}, name="区域查看", desc="查看区域信息")
	public ModelAndView edit(String id) {
		ModelAndView mv = new ModelAndView("area/edit");
		if (Utils.isNotBlank(id)) {
			Area area = this.baseService.get(Area.class, id);
			mv.addObject("entity", area);
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/save")
	@RequirePermission(value={"AREA-MODIFY"}, name="区域修改", desc="修改区域信息")
	public RespData save(Area area) {
		return areaService.save(area);
	}
	
	@ResponseBody
	@RequestMapping("/del")
	@RequirePermission(value={"AREA-DEL"}, name="区域删除", desc="删除区域信息")
	public RespData del(String[] ids) {
		return areaService.del(ids);
	}
	
	@ResponseBody
	@RequestMapping("/state")
	@RequirePermission(value={"AREA-STATE"}, name="区域状态修改", desc="修改区域状态")
	public RespData state(String[] ids, Integer state) {
		return this.state(Area.class, ids, state);
	}
}
